package com.stormrage.edu.serv.edu.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.edu.EduChapterDao;
import com.stormrage.edu.serv.edu.EduChapterServ;
import com.stormrage.edu.serv.edu.EduVideoServ;
import com.stormrage.entity.edu.EduChapter;
import com.stormrage.entity.edu.rep.EduChapterRep;
import com.stormrage.entity.edu.rep.EduVideoRep;
import com.stormrage.entity.learn.req.LeaStudentVideoReq;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: EduChapterServImpl.java
 * @Package com.stormrage.edu.serv.edu.impl
 * @Description: 章节 Service
 * @author www.stormragetech.com
 * @date 2016年4月8日
 * @version V1.0
 */
@Service
public class EduChapterServImpl implements EduChapterServ {

	private static final Logger logger = LoggerFactory.getLogger(EduMajorServImpl.class);

	@Autowired
	private EduChapterDao eduChapterDao;

	@Autowired
	private EduVideoServ eduVideoServ;

	@Override
	public EduChapter queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduChapter entity = eduChapterDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public List<EduChapterRep> queryRepListByCourseId(final LeaStudentVideoReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 queryRepListByCourseId 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0.getCourseId());
		map.put("stuId", arg0.getStuId());

		List<EduChapterRep> array = eduChapterDao.queryRepListByCourseId(map);
		int seq = 1;

		Iterator<EduChapterRep> it = array.iterator();
		while (it.hasNext()) {
			EduChapterRep entity = it.next();
			arg0.setChapId(entity.getId());
			List<EduVideoRep> list = eduVideoServ.queryRepListByChapId(arg0);
			logger.debug(" 返回结果为 " + list);
			entity = EduChapterRep.instance(entity, seq, list);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 queryRepListByCourseId 】- 完成运行 ");
		return array;
	}

}
